import type { App } from 'vue'
import { nextTick } from 'vue'
import { routeMode } from '@jsjn/micro-core-micro-main/routeMode'

export default (app: App) => {
    app.directive('auth', {
        mounted(el: HTMLElement, binding) {
            if (routeMode !== 'backEnd') return false

            const btnPerms = binding.value
            const currentModuleBtnsConfig = app.config.globalProperties.$route.meta.btns

            // 当前模块配置了按钮功能点
            if (currentModuleBtnsConfig && currentModuleBtnsConfig.length > 0) {
                const currentBtnConfig = currentModuleBtnsConfig.find(
                    (btn) => btn.perms === btnPerms
                )

                // 查到了对应的功能点按钮
                if (currentBtnConfig && !currentBtnConfig.btnCheck) {
                    nextTick(() => {
                        el.parentNode.removeChild(el)
                    })
                }
            }
        }
    })
}
